package work.sby.web;


import work.sby.pojo.Brand;
import work.sby.pojo.Buy;
import work.sby.pojo.User;
import work.sby.service.BrandService;
import work.sby.service.BuyService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/addBuy")
public class AddBuy extends HttpServlet{

    private BuyService service =new BuyService();
    private BrandService brandService = new BrandService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        HttpSession session = request.getSession();
        String goodname = java.net.URLDecoder.decode(request.getParameter("goodname"), "UTF-8");
        System.out.println(goodname);
        byte[] bytes = goodname.getBytes("ISO-8859-1");

        // 将字节数组按UTF-8编码转换回正确的字符串
        String tgoodname = new String(bytes, "UTF-8");
        User user = (User) session.getAttribute("user");
        //1.调用Brandservice
        //2.根据用户名查询购买记录
        String buyername=user.getUsername();
        service.addbuy(buyername,tgoodname);


        request.getRequestDispatcher("/selectByBrandName").forward(request,response);

    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}